Beginning Python

Getting started

Writing our first script

A Python script is a simple text file containing a set of instructions. As long as these instructions are written in the "Python Programming Language" then the computer will be able to understand them and perform the tasks we've asked.

To create our script, go to the text editor pane in JupyterLab and type into it:

print("Hello from Python")

then go to File → Save File and you'll see the small black circle next to the words "untitled.txt" in the tab change to a cross.

We've just saved our Python script with the name "untitled.txt" but we should give it a more descriptive name. In the far left pane you will see a list of files. Right click on "untitled.txt" and select Rename. Python files end in the extension ".py" so change its name to "script.py".

You should now see that the name of the text editor tab reads script.py.

Running our script

To run the script that we've just written and saved, we move to the pane on the right with the label "Terminal 1". This pane gives us access to a command line interface to our computer. This is the same interface that you have in a Command Prompt in Windows or a Terminal on MacOS or Linux. We type commands in here and the computer runs the programmes we ask it to.

The only command we need to know about here is python which we run by typing python in that window, followed by a space, followed by the name of our script file (including the ".py" extension) and then pressing enter:

python script.py

If you do that you should see, printed to the terminal:

Hello from Python

Congratulations, you've just written and executed your very first Python programme!

If you see an error here then you may need to use a slightly different command to run Python. Try running your script with python3 script.py (note the "3") as described in this video.

Explanation

This was a simple script. Python is a language designed to help you write everything from small and simple scripts to large complete programs. In my opinion Python is one of the best prototyping languages, and the best language for writing programs that glue together or provide interfaces to other programs.

This script has introduced three of the basic building blocks of Python;

  • A function print. Functions are instructions that tell Python to do something, in this case print tells Python to print the following string to the screen. Functions are "called" by following them with a pair of parentheses (). Whatever we put between those parentheses are called "arguments" and specify exactly what the function should do.
  • A string "Hello from Python!". A string is just a piece of text, which can contain multiple lines. Strings are enclosed in double quotes.
  • A line of code print("Hello from Python!"). A line of code forms a complete instruction which can be executed by Python. Python executes each line of code, one at a time in order, moving from the top of the file downwards until it reaches the end of the file.

Important things to remember for this section are:

  • Always save your Python scripts after making a change (the black circle means there are unsaved changes)
  • Always give your Python scripts a ".py" extension
  • Python code goes in the text editor window while you run the script from the terminal window

How to read these notes

In these notes, any time that we're seeing a full Python script, we'll see it written like the following:

script.py
print("Hello from Python!")

Here, the name that we give the file (script.py) is in the left-hand margin and the contents of the file (print("Hello from Python")) are alongside it.

We'll denote "run in the Terminal" with a $ sign in the left margin. So when we ask you to run your script, it will look like this:

$
python script.py
Hello from Python!

And we see the expected output displayed below it.

By contrast, any time we're just showing you a small snippet of code, for example to demonstrate a point, it will be shown like:

print("Hello from Python!")